﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;

using DM_Article.Model;

namespace DM_Article.Bll
{
    public class adminbll
    {
        public static int add(OleDbConnection conn,admin model)
        {
            OleDbCommand odc = new OleDbCommand();
            odc.CommandText = "INSERT INTO admin(username, [password], usertype) VALUES ('" + model.username + "', '" + model.password + "'," + model.usertype + ")";
            odc.Connection = conn;
            return odc.ExecuteNonQuery();
        }

        public static int update(OleDbConnection conn, admin model)
        {
            OleDbCommand odc = new OleDbCommand();
            odc.CommandText = "update admin set username ='" + model.username + "',[password]='" + model.password + "',usertype=" + model.usertype + " where userid=" + model.userid;
            odc.Connection = conn;
            return odc.ExecuteNonQuery();
        }


        public static int delete(OleDbConnection conn, string userid)
        {
            OleDbCommand odc = new OleDbCommand();
            odc.CommandText = "delete from admin where userid=" + userid;
            odc.Connection = conn;
            return odc.ExecuteNonQuery();
        }

        public static admin getAdminDetailByName(OleDbConnection conn, string name)
        {
            OleDbDataAdapter odda = new OleDbDataAdapter();
            odda.SelectCommand = new OleDbCommand();
            odda.SelectCommand.CommandText = "select [userid],[username],[password],[usertype] from admin where username='" + name + "'";
            odda.SelectCommand.Connection = conn;
            DataTable dt = new DataTable();
            odda.Fill(dt);

            admin model = new admin();
            if (dt.Rows.Count > 0)
            {
                if (dt.Rows[0]["userid"].ToString() != "")
                {
                    model.userid = Convert.ToInt32(dt.Rows[0]["userid"].ToString());
                }
                if (dt.Rows[0]["username"].ToString() != "")
                {
                    model.username = dt.Rows[0]["username"].ToString();

                }
                if (dt.Rows[0]["password"].ToString() != "")
                {
                    model.password = dt.Rows[0]["password"].ToString();

                }
                if (dt.Rows[0]["usertype"].ToString() != "")
                {
                    model.usertype = dt.Rows[0]["usertype"].ToString();

                }
            }
            return model;
        }

        public static admin getAdminDetailById(OleDbConnection conn, string id)
        {
            OleDbDataAdapter odda = new OleDbDataAdapter();
            odda.SelectCommand = new OleDbCommand();
            odda.SelectCommand.CommandText = "select [userid],[username],[password],[usertype] from admin where userid=" + id;
            odda.SelectCommand.Connection = conn;
            DataTable dt = new DataTable();
            odda.Fill(dt);

            admin model = new admin();
            if (dt.Rows.Count > 0)
            {
                if (dt.Rows[0]["userid"].ToString() != "")
                {
                    model.userid = Convert.ToInt32(dt.Rows[0]["userid"].ToString());
                }
                if (dt.Rows[0]["username"].ToString() != "")
                {
                    model.username = dt.Rows[0]["username"].ToString();

                }
                if (dt.Rows[0]["password"].ToString() != "")
                {
                    model.password = dt.Rows[0]["password"].ToString();

                }
                if (dt.Rows[0]["usertype"].ToString() != "")
                {
                    model.usertype = dt.Rows[0]["usertype"].ToString();

                }
            }
            return model;
        }

        public static DataTable getAdminTable(OleDbConnection conn)
        {
            OleDbDataAdapter odda = new OleDbDataAdapter();
            odda.SelectCommand = new OleDbCommand();
            odda.SelectCommand.CommandText = "select [userid],[username],[password],[usertype] from admin";
            odda.SelectCommand.Connection = conn;
            DataTable dt = new DataTable();
            odda.Fill(dt);
            return dt;
        }

        public static DataTable checkAdminPassword(OleDbConnection conn, string username)
        {
            OleDbDataAdapter odda = new OleDbDataAdapter();
            odda.SelectCommand = new OleDbCommand();
            odda.SelectCommand.CommandText = "select password from admin where username='" + username + "'";
            odda.SelectCommand.Connection = conn;
            DataTable dt = new DataTable();
            odda.Fill(dt);
            return dt;
        }
    }
}
